package com.bdqn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.base.R;
import com.bdqn.base.logger.WebLogger;
import com.bdqn.pojo.ShopActivity;
import com.bdqn.service.ShopActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 活动
 *
 * @author lilibo
 * @date 2021/10/09
 */
@Api(tags="活动接口")
@RestController
@RequestMapping("/shopactivity")
public class ShopActivityController {

    @Autowired
    private ShopActivityService shopActivityService;

    /**
     * 新增活动记录
     *
     * @param shopActivity
     * @return R
     */
    @ApiOperation("新增活动记录")
    @WebLogger("新增活动记录")
    @PostMapping("/add")
    public R save(@ApiParam("活动对象") @RequestBody ShopActivity shopActivity) {
        return new R<>(shopActivityService.save(shopActivity));
    }

    /**
     * 修改活动记录
     *
     * @param shopActivity
     * @return R
     */
    @ApiOperation("修改活动记录")
    @WebLogger("修改活动记录")
    @PostMapping("/update")
    public R update(@ApiParam("活动对象") @RequestBody ShopActivity shopActivity) {
        return new R<>(shopActivityService.updateById(shopActivity));
    }

    /**
     * 通过id删除一条活动记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id删除一条活动记录")
    @WebLogger("通过id删除一条活动记录")
    @PostMapping("/delete")
    public R removeById(@ApiParam("活动id") Integer id) {
        return new R<>(shopActivityService.removeById(id));
    }

    /**
     * 通过id查询单条活动记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id查询单条活动记录")
    @WebLogger("通过id查询单条活动记录")
    @GetMapping("/{id}")
    public R<ShopActivity> getById(@ApiParam("活动id") @PathVariable Integer id) {
        return new R<>(shopActivityService.getById(id));
    }

    /**
     * 通过活动信息查询活动记录
     *
     * @param shopActivity 活动信息对象
     * @return R
     */
    @ApiOperation("通过活动信息查询活动记录")
    @WebLogger("通过活动信息查询活动记录")
    @GetMapping("/list")
    public R<List<ShopActivity>> getList(@ApiParam("查询的活动对象") ShopActivity shopActivity) {
        return new R<>(shopActivityService.getList(shopActivity));
    }

    /**
     * 分页查询活动
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopActivity 活动
     * @return
     */
    @ApiOperation("分页查询活动")
    @WebLogger("分页查询活动")
    @GetMapping("/page")
    public R<IPage<ShopActivity>> getPage(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的活动对象") ShopActivity shopActivity) {
        return new R<>(shopActivityService.getPage(new Page<>(pageNo, pageSize), shopActivity));
    }

    /**
     * 分页查询活动数据
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopActivity 活动
     * @return 返回对象中的count为数据总量
     */
    @ApiOperation("分页查询活动数据")
    @WebLogger("分页查询活动数据")
    @GetMapping("/pagelist")
    public R<List<ShopActivity>> getPageList(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的活动对象") ShopActivity shopActivity) {
        IPage<ShopActivity> page = shopActivityService.getPage(new Page<>(pageNo, pageSize), shopActivity);
        return new R<>(page.getRecords(), page.getTotal());
    }

}
